package cn.iocoder.yudao.module.gov.controller.admin.resourcedirectory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.gov.controller.admin.resourcedirectory.vo.*;
import cn.iocoder.yudao.module.gov.dal.dataobject.resourcedirectory.ResourceDirectoryDO;
import cn.iocoder.yudao.module.gov.service.resourcedirectory.ResourceDirectoryService;

@Tag(name = "管理后台 - 政务资源目录")
@RestController
@RequestMapping("/gov/resource-directory")
@Validated
public class ResourceDirectoryController {

    @Resource
    private ResourceDirectoryService resourceDirectoryService;

    @PostMapping("/create")
    @Operation(summary = "创建政务资源目录")
    @PreAuthorize("@ss.hasPermission('gov:resource-directory:create')")
    public CommonResult<Long> createResourceDirectory(@Valid @RequestBody ResourceDirectorySaveReqVO createReqVO) {
        return success(resourceDirectoryService.createResourceDirectory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新政务资源目录")
    @PreAuthorize("@ss.hasPermission('gov:resource-directory:update')")
    public CommonResult<Boolean> updateResourceDirectory(@Valid @RequestBody ResourceDirectorySaveReqVO updateReqVO) {
        resourceDirectoryService.updateResourceDirectory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除政务资源目录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('gov:resource-directory:delete')")
    public CommonResult<Boolean> deleteResourceDirectory(@RequestParam("id") Long id) {
        resourceDirectoryService.deleteResourceDirectory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除政务资源目录")
                @PreAuthorize("@ss.hasPermission('gov:resource-directory:delete')")
    public CommonResult<Boolean> deleteResourceDirectoryList(@RequestParam("ids") List<Long> ids) {
        resourceDirectoryService.deleteResourceDirectoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得政务资源目录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('gov:resource-directory:query')")
    public CommonResult<ResourceDirectoryRespVO> getResourceDirectory(@RequestParam("id") Long id) {
        ResourceDirectoryDO resourceDirectory = resourceDirectoryService.getResourceDirectory(id);
        return success(BeanUtils.toBean(resourceDirectory, ResourceDirectoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得政务资源目录分页")
    public CommonResult<PageResult<ResourceDirectoryRespVO>> getResourceDirectoryPage(@Valid ResourceDirectoryPageReqVO pageReqVO) {
        PageResult<ResourceDirectoryDO> pageResult = resourceDirectoryService.getResourceDirectoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResourceDirectoryRespVO.class));
    }
    @GetMapping("/getAll")
    @Operation(summary = "获得所有政务资源目录")
    public CommonResult<List<ResourceDirectoryRespVO>> getResourceDirectoryAll() {
        List<ResourceDirectoryDO> Result = resourceDirectoryService.getResourceDirectoryAll();
        return success(BeanUtils.toBean(Result, ResourceDirectoryRespVO.class));
    }
    @PutMapping("/update-status")
    @Operation(summary = "修改目录状态")
    public CommonResult<Boolean> updateResourceDirectoryStatus(@Valid @RequestBody ResourceDirectoryPageReqVO reqVO) {
        resourceDirectoryService.updateResourceDirectoryStatus(reqVO.getCatalogId(), reqVO.getStatus());
        return success(true);
    }
    @Operation(summary = "设置接口")
    @PostMapping("/set-interface")
    public CommonResult<Boolean> setInterface(@Valid @RequestBody ResourceDirectoryPageReqVO reqVO) {
        resourceDirectoryService.Interface(reqVO.getCatalogId(), reqVO.getInfoId());
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出政务资源目录 Excel")
    @PreAuthorize("@ss.hasPermission('gov:resource-directory:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportResourceDirectoryExcel(@Valid ResourceDirectoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceDirectoryDO> list = resourceDirectoryService.getResourceDirectoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "政务资源目录.xls", "数据", ResourceDirectoryRespVO.class,
                        BeanUtils.toBean(list, ResourceDirectoryRespVO.class));
    }

}